package com.readpost.mapper;


import com.readpost.doman.Entity.User;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Select;

@Mapper
public interface UserMapper{

    //通过id查询用户
    @Select("select * from users where user_id = #{id}")
    User getUserById(int id);

    //通过用户名查询用户
    @Select("select * from users where username = #{username}")
    User getUserByUsername(String username);

    //通过邮箱查询用户
    @Select("select * from users where email = #{email}")
    User getUserByEmail(String email);

    //通过手机号查询用户
    @Select("select * from users where phone = #{phone}")
    User getUserByPhone(String phone);

    //登录验证
    @Select("select * from users where username = #{username} and password_md5 = #{password}")
    User getUserByUsernameAndPassword(String username, String password);

    //注册用户
    @Insert("insert into users(username,password_md5,created_at,updated_at)" +
            "values(#{username},#{md5Password},now(),now())")
    void insertUser(String username, String md5Password);

    //更新用户基本信息
    @Select("update users set username = #{username},email = #{email},bio = #{bio},phone = #{phone},updated_at = now() where user_id = #{userid}")
    void updateUser(User user, Integer userid);

    //更新用户头像
    @Select("update users set avatar_url = #{avatarUrl},updated_at = now() where user_id = #{userid}")
    void updatePicture(String avatarUrl, Integer userid);

    //更新用户密码
    @Select("update users set password_md5 = #{md5Password},updated_at = now() where user_id = #{userid}")
    void updatePwd(String md5Password, Integer userid);
}
